    /*
 * Copyright (c) 2022, yifengling0. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. No personal names or organizations' names associated with the
 *    Atomthreads project may be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE ATOMTHREADS PROJECT AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "atom.h"
#include "pin.h"
#include "debug.h"
#include "drv_pin.h"
#include "syslog.h"
#include <string.h>
#include "mraa/gpio.h"


#define _UPBOARD_PIN(index, gpio, comment, context ) \
    {                             \
        index, gpio, comment, context \
    }

struct pin_index
{
    int index;
    int gpio;
    const char* comment;
    mraa_gpio_context context;
};

//13      GPIO27: GPIO
//16      GPIO23 : GPIO
//18      GPIO24 : GPIO
//22      GPIO25 : GPIO
//29       GPIO5 : GPIO
//31       GPIO6 : GPIO
//37      GPIO26 : GPIO


static struct pin_index pins[] = 
{
    _UPBOARD_PIN(7 ,  0, "IO7", NULL),
    _UPBOARD_PIN(29 ,  2, "IO29", NULL),
    _UPBOARD_PIN(31 ,  3, "IO31", NULL),
    _UPBOARD_PIN(36 ,  27, "IO36", NULL ),
    _UPBOARD_PIN(37 ,  13, "IO37", NULL ),
};


#define ITEM_NUM(items) sizeof(items) / sizeof(items[0])
static struct pin_index *get_pin(int pin)
{
    for (int i = 0; i < ITEM_NUM(pins); i++) {
        if (pins[i].index == pin) {
            return (&pins[i]);
        }
    }

    return NULL;
};


int  upboard_read( const Pin* dev, int pin )
{
    int value;
    const struct pin_index *index;

    value = PIN_LOW;

    index = get_pin(pin);
    if (index == NULL)
    {
        SYSLOG(LOG_WARNING, "pin no %d not found!", pin);
        return NG;
    }

    value = mraa_gpio_read(index->context);
    
    if(value > 0) return PIN_HIGH;
    else return PIN_LOW;
}


void upboard_write( const Pin* dev, int pin, int value )
{
    const struct pin_index *index;

    index = get_pin(pin);
    if (index == NULL)
    {
        SYSLOG(LOG_WARNING, "pin no %d not found!", pin);
        return;
    }


    mraa_gpio_write(index->context, value);
}

void upboard_toggle( const Pin* dev, int pin)
{
    const struct pin_index *index;

    index = get_pin(pin);
    if (index == NULL)
    {
        SYSLOG(LOG_WARNING, "pin no %d not found!", pin);
        return;
    }

    mraa_gpio_write(index->context,!mraa_gpio_read(index->context));
}

void upboard_list(const Pin* dev)
{
    KPrint("GPIO list : \r\n");
    for (int i = 0; i < sizeof(pins)/sizeof(pins[0]); i++) {
        KPrintf("  %02d : %s\r\n", pins[i].index, pins[i].comment);
    }
}

void upboard_mode( const Pin* dev, int pin, int mode)
{
     struct pin_index *index;
     mraa_gpio_mode_t mraa_mode;
     mraa_gpio_dir_t dir;

    index = get_pin(pin);
    if (index == NULL)
    {
        SYSLOG(LOG_WARNING, "pin no %d not found!", pin);
        return;
    }

     if (mode == PIN_MODE_OUTPUT)
     {
         /* output setting */
         dir = MRAA_GPIO_OUT;
         mraa_mode = MRAA_GPIO_STRONG;
     }
     else if (mode == PIN_MODE_INPUT)
     {
         /* input setting: not pull. */
         dir = MRAA_GPIO_IN;
         mraa_mode = MRAA_GPIOD_OPEN_DRAIN;
     }
     else if (mode == PIN_MODE_INPUT_PULLUP)
     {
         dir = MRAA_GPIO_IN;
         mraa_mode = MRAA_GPIO_PULLUP;
     }
     else if (mode == PIN_MODE_INPUT_PULLDOWN)
     {
         dir = MRAA_GPIO_IN;
         mraa_mode = MRAA_GPIO_PULLDOWN;
     }

     else if (mode == PIN_MODE_OUTPUT_OD)
     {
         dir = MRAA_GPIO_OUT;
         mraa_mode = MRAA_GPIOD_OPEN_DRAIN;
     }

     mraa_gpio_dir(index->context, dir);
     mraa_gpio_mode(index->context, mraa_mode);
}

static int upboard_attach_irq(const Pin* dev, int pin, int mode, void (*hdr)(void* args), void* args)
{
    return ATOM_OK;
}

static int upboard_detach_irq(const Pin* dev, int pin)
{
    return ATOM_OK;
}

static int upboard_irq_enable(const Pin* dev, int pin, int enabled)
{
    return ATOM_OK;
}

static struct PinOps _pin_ops = {
    upboard_read,
    upboard_write,
    upboard_toggle,
    upboard_mode,
    upboard_list,
    upboard_attach_irq,
    upboard_detach_irq,
    upboard_irq_enable
};

void upboard_gpio_init()
{
     for (int i = 0; i < sizeof(pins)/sizeof(pins[0]); i++) {
        pins[i].context = mraa_gpio_init(pins[i].index);
        //mraa_gpio_dir(pins[i].context, MRAA_GPIO_OUT);
    }   
}

void UpPinCreate(void)
{
    PinCreate("pin", &_pin_ops);
    upboard_gpio_init();
}